package ch13;

import java.util.ArrayList;

// AddOrDeleteAccount.java
// Represents the AddAccount and DeleteAccount option

public class AddOrDeleteAccount extends Option {
	private ArrayList<String> stringArray = new ArrayList<String>();
	private Keypad keypad;
	BankDatabase bankDatabase = getBankDatabase();
	int x = 1;
	ArrayList<Customer> customers = bankDatabase.getCustomerList();

	// AddOrDeleteAccount constructor
	public AddOrDeleteAccount(int userID, Screen atmScreen,
			BankDatabase atmBankDatabase, Keypad atmKeypad) {
		super(userID, atmScreen, atmBankDatabase);

		keypad = atmKeypad;
	} // end AddOrDeleteAccount constructor

	// creates a new Account in the ATM or deletes a account from the ATM
	public void execute() {
		stringArray.clear();
		stringArray.add("Press 1: Add account to customer");
		stringArray.add("Press 2: Delete a customers account ");
		screen.setKasse(stringArray, 10);
		int choose = keypad.getInput();

		if (choose == 1) {
			NewAccount();
		}
		if (choose == 2) {
			DeleteAccount();
		}
	} // end execute method

	// method for adding a new account to customer
	public void NewAccount() {
		stringArray.clear();
		stringArray.add("Enter a CustomerID you want to add a new Account: ");
		screen.setKasse(stringArray, 10);
		int customerID = keypad.getInput();

		// loop through customers and see if customer exist
		for (Customer currentCustomer : customers) {
			// if customer exist x equals 1
			if (currentCustomer.getCustomerID() == customerID) {
				x = 1;
				break;
			} else
				// else not x equals 3
				x = 3;

		}// end for loop

		if (x == 3) {// if x equals 3 print out username dosn't exist
			stringArray.clear();
			stringArray.add("username dosn't exist");
			screen.setKasse(stringArray, 10);
		} // end if
		if (x == 1) {
			stringArray.clear();
			stringArray.add("Enter Type of Accounttype:  ");
			stringArray.add("Ex. MainAccount ");
			stringArray.add("Ex. LoanAccount ");
			stringArray.add("Ex. SavingsAccount");
			stringArray.add("Ex. CarLoanAccount");
			screen.setKasse(stringArray, 10);

			String type = keypad.getInputS();

			double interest = getInterest(type);

			System.out.println("\nEnter balance: ");
			int availableBalance = keypad.getInput();

			int overdraft = 0;
			if (type == "MainAccount") {
				System.out.println("Enter overdraft: ");
				overdraft = keypad.getInput();
			}// end if
			int totalBalance = availableBalance + overdraft;

			bankDatabase.getCustomer(customerID).addAccounts(
					new Account(type, availableBalance, totalBalance, interest,
							overdraft));
			screen.displayMessageLine("A " + type + " to " + customerID
					+ " has ben added");
		} // end if

	}// end NewAccount method

	// method for deleting account
	public void DeleteAccount() {
		
		stringArray.clear();
		stringArray.add("Enter the CustomerID you wish to delete an account");
		screen.setKasse(stringArray, 10);
		int customerID = keypad.getInput();
		
		// loop through customers and see if customer exist
				for (Customer currentCustomer : customers) {
					// if customer exist x equals 1
					if (currentCustomer.getCustomerID() == customerID) {
						x = 1;
						break;
					} else
						// else not x equals 3
						x = 3;
				}// end for loop
				
				if (x == 3) {// if x equals 3 print out username dosn't exist
					stringArray.clear();
					stringArray.add("username dosn't exist ");
					screen.setKasse(stringArray, 10);
				} // end if
				if (x==1){

		ArrayList<Account> accounts = bankDatabase.getCustomer(customerID)
				.getAccounts();// save current customer accounts to arraylist
								// accounts
		stringArray.clear();
		// printout customers account type, totalbalance and availablebalance
		stringArray.add("All customers account:");
		for (int i = 0; i < accounts.size(); i++) {
			stringArray.add(i + 1 + ": Account Type:  "
					+ accounts.get(i).getType());
			// screen.setKasse(stringArray, 10);

		}
		stringArray.add("");
		stringArray.add("Choose the account you want to delete: ");
		screen.setKasse(stringArray, 10);
		int delAccount = keypad.getInput() - 1;
		
		accounts.remove(delAccount);
		
		stringArray.clear();
		stringArray.add("Account is deleted");
		screen.setKasse(stringArray, 10);
				}
	}// end Delete account method

	// Admin calls this method when interest is needed
	private double getInterest(String type) {
		double interestRate = 0;
		switch (type) {
		case "MainAccount":
			interestRate = 1.5;
		case "LoanAccount":
			interestRate = 15;
		case "SavingsAccount":
			interestRate = 3;
		case "HomeLoanAccount":
			interestRate = 5.5;
		case "CarLoanAccount":
			interestRate = 7.3;
			break;
		default:
			stringArray.clear();
			stringArray.add("Not predefined Account");
			stringArray.add("Type in interestRate for this account: ");
			screen.setKasse(stringArray, 10);
			double input = keypad.getInput();
			interestRate = input;

		}// end switch for getting rate
		return interestRate;
	} // end method getInterest

} // end class AddOrDeleteAccount